﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TicketingCompany
{
    public partial class EditEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id;
            if (Request["id"] != null && int.TryParse(Request["id"].ToString(), out id)&& !Page.IsPostBack)
            {
                UpdatePanelEvent.Visible = true;
                TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
                var ev = dbContext.Events.Single(u => u.EventID == id);
                DropDownListCategories.SelectedValue = ev.CategoryID.ToString();
                DropDownListLocations.SelectedValue = ev.LocationID.ToString();
                TextBoxEventTitle.Text = ev.Title;
                RadDatePickerEvent.SelectedDate = DateTime.Now;
                TextBoxPrice.Text = ev.Price.ToString();
            }

        }

        protected void ButtonModifyEvent_Click(object sender, EventArgs e)
        {
            if (TextBoxEventTitle.Text != "" && RadDatePickerEvent.SelectedDate > DateTime.MinValue)
            {
                decimal money;

                if (0 > TextBoxEventTitle.Text.Length &&
                    TextBoxEventTitle.Text.Length > 150)
                {
                    LabelInvalidTitle.Visible = true;
                }
                else if (!decimal.TryParse(TextBoxPrice.Text, out money))
                {
                    LabelErrorCreate.Text = "Invalid Price!";
                    LabelErrorCreate.Visible = true;
                }
                else
                {
                    TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
                    int id = int.Parse(Request["id"]);
                    Event ev = dbContext.Events.Single(eve => eve.EventID == id);

                    ev.CategoryID = int.Parse(DropDownListCategories.SelectedValue);
                    ev.LocationID = int.Parse(DropDownListLocations.SelectedValue);
                    ev.Title = TextBoxEventTitle.Text;
                    ev.DateTime = DateTime.Parse(RadDatePickerEvent.SelectedDate.ToString());
                    ev.Price = money;
                    dbContext.SaveChanges();
                    dbContext.Dispose();
                    LabelErrorCreate.Text = "Modify Successful!";
                    LabelErrorCreate.Visible = true;
                }
            }
            else
            {
                LabelErrorCreate.Visible = true;
            }
        }
    }
}